var map = new Map();

$(document).ready(function() {
    showLoading();
    $('title', parent.document).html($('title').text());
	setBaseUrl();
	firstSearch();
});

function firstSearch() {
	var roleIDElement = window.parent.document.getElementById("roleID");
	if (roleIDElement && roleIDElement.value) {
		$("#roleID").val(roleIDElement.value);
	    window.parent.document.getElementById("roleID").value = '';
	}
	search();
}
function search(){
	showLoading();
	$("#save").hide();
	if (!map.get($("#roleID").val())) {
		$ajaxCommon('searchRoleResource', searchRoleResource, {RoleID: $("#roleID").val()});
	} else {
		searchRoleResource();
	}
}

function searchRoleResource(data){
	if (data && data.roleResourceList && data.roleResourceList.length > 0) {
		var roleNameElement = window.parent.document.getElementById("RoleName");
		var ul = "<ul><li style='background-color:#ECECEC'><span style='color: #09be09;'>权限名称列表(" + ((roleNameElement && roleNameElement.value) || '角色名称异常') + ")</span> " 
					+ "<a style='float: right;margin-right: 10px;' class='detalis' onclick='showAllLi()'>展开全部</a> "
					+ "<a style='float: right;margin-right: 10px;' class='detalis' onclick='hideAllLi()'>隐藏全部</a>"
					+ "</li>"
					+ "</ul>";
		ul += createTree(roleResourceBody(data.roleResourceList), 0);
		$("#treeMenu").html('');
	    $("#treeMenu").append(ul);
	    //控制菜单的隐藏显示
	    $("ul[class] li span[class]").each(function(){
		    $(this).click(function(){
		    	$(this).find('img#userSelectRight').toggle();
		    	$(this).find('img#userSelectBottom').toggle();
		    	$(this).parent().next().children("li").toggle();
		    }) ;
	    }) ;
	    map.set($("#roleID").val(), ul);
	    $("#save").show();
	} else {
		if (!map.get($("#roleID").val())) {
			$("#treeMenu").html('');
		    $("#treeMenu").append(map.get($("#roleID").val()));
		    $("#save").show();
		} else {
			$("#treeMenu").html('角色权限数据不存在！')
		}
	}
    setTimeout(hideLoading, 500);
}

function roleResourceBody(data,zNodes){
	var treeNodes =[];
	var dateLength = eval(data).length;
	if (dateLength == 0) {
	  showMessge(messageContent["Search.NoResultFound"]);
    } else {
    	for(var j=0; j<dateLength; j++){
		  treeNodes.push({
			"id" : eval(data)[j].resourcesID,
			"pid" : eval(data)[j].parentID,
			"name" : eval(data)[j].resourcesName + (eval(data)[j].roleName ? '' : "(没有权限)"),
			"checked" : eval(data)[j].roleID == "NotFound" ? "" : "checked" ,
	//		"click" : "child("+searchResourceData[j].resourcesID+",this)"
		});
		  }
	}
	return treeNodes;
}
//主方法，运用递归实现
function createTree(jsons, pid){
    if(jsons != null){
        var ul = "<ul class=''>" ;
        for(var i=0;i<jsons.length;i++){
            if(jsons[i].pid == pid){
            	if (jsons[i].pid == 0) {
            		ul += '<li><input name="chk" id="'+ jsons[i].id +'" type="checkbox"'+ jsons[i].checked +'><span class="clickUL">'
            			+ jsons[i].name 
            			+ " <img id='userSelectRight' src='../static/images/userSelectRight.png' width='12' height='12'/>"
            			+ " <img id='userSelectBottom' src='../static/images/userSelectBottom.png' width='12' height='12' hidden='hidden'/></span>"
            			+ "</li>" ;
            	} else {
            		ul += '<li style="display:none;"><input name="chk" id="'+ jsons[i].id +'" type="checkbox"'+ jsons[i].checked +'><span class="clickUL">' 
            			+ jsons[i].name 
            			+ " <img id='userSelectRight' src='../static/images/userSelectRight.png' width='12' height='12'/>"
            			+ " <img id='userSelectBottom' src='../static/images/userSelectBottom.png' width='12' height='12' hidden='hidden'/></span>"
            			+ "</li>" ;
            	}
                ul += createTree(jsons,jsons[i].id) ;
            }
        }
        ul += "</ul>" ;
    }
    return ul ;
}
function showAllLi() {
	$("ul[class] li span").each(function(){
    	$(this).parent().next().children("li").show();
    	$(this).parent()[0].childNodes[1].childNodes[1].src = "../static/images/userSelectBottom.png";
    }) ;
}
function hideAllLi() {
	$("ul[class] li span").each(function(){
    	$(this).parent().next().children("li").hide();
    	$(this).parent()[0].childNodes[1].childNodes[1].src = "../static/images/userSelectRight.png";
	}) ;
}
function savePermissions(){
	var saveResourceID = [];
	var deleteResourceID = [];
	var nodes = document.getElementsByName('chk');
	var length = nodes.length;
	if(length == 0){
		showMessge(messageContent["Update.NotChange"]);
	}else{
		for (var i=0; i<length; i++) {
			if (nodes[i] && nodes[i].checked) {
				saveResourceID.push(nodes[i].id);
			} else {
				deleteResourceID.push(nodes[i].id);
			}
		}
		$ajax("saveRoleResources", 
			{
				RoleID: $("#roleID").val(), 
				saveResourceID: saveResourceID.toString(), 
				deleteResourceID: deleteResourceID.toString()
			}, 
			saveRoleResources
		);
	}
}

function saveRoleResources(data){
    var result = data && data.result;
    if (result == "Success") {
    	map.set($("#roleID").val(), undefined);
    	search();
		showMessge(messageContent["Update.Success"]);
	} else if (result == "NotFound") {
		showMessge(messageContent["Update.NotFound"]);
	} else {
		showMessge(messageContent["Update.Error"]);
	}
}
